package com.cheng.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;


public class RSA {
	private static String module = "5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=";   
    private static String exponentString = "AQAB";   
    private static String delement = "vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=";   
    private static String encryptString = "Vx/dGjS1YWKRubsoDgiShiwLgqyNE2z/eM65U7HZx+RogwaiZimNBxjuOS6acEhKZx66cMYEAd1fc6oewbEvDIfP44GaN9dCjKE/BkkQlwEg6aTO5q+yqy+nEGe1kvLY9EyXS/Kv1LDh3e/2xAk5FNj8Zp6oU2kq4ewL8kK/ai4="; 
    private static String pc = "lb01Tl5w4b7Lsce+Qhlx/ickieAtfOTDLuiLe9sYRRWYyQCPL524qDJZ2Tc0DcU5QITzB8wKKJDDiYHTpM3CogJK2PWq3RGZBlTOw8Gd9sfj4Ygv81k6VhVEfEtb4vYUepdCbeP0ItedULzHk+u3rsEWDxoFIsLLSqiuskUaBlw=";
    private static String an = "f0rJuJW2EYDxcv/0wu41UHQhpurUml/XWpXFwWUYF5KkpnO1fx9xc2qEzio+84Z19gRaT6mpoMQBzhBKlxPTpI9yJoUzXELliEyp/+XUjKQuFgN+0buTqYjb42NqU9GVtegT0b4XYB+mhco1cYuE3SBE6Orl+NZu48F9aAjKmYU=";

    /**   
     * @param args   
     */   
    public static void main(String[] args) {   
        byte[] en = encrypt("chenhailong");   
        System.out.println(BASE64.encodeByteToString(en));   
        byte[] enTest = null;   
        try {
			enTest = BASE64.decodeStringToByteArr(pc);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}      
        System.out.println(enTest.length);   
        System.out.println(en.length);   
        System.out.println(new String(Dencrypt(en)));   
        System.out.println(new String(Dencrypt(enTest)));   
    }   
  
    public static byte[] encrypt(String ss) {   
        try {   
            byte[] modulusBytes = BASE64.decodeStringToByteArr(module);   
            byte[] exponentBytes = BASE64.decodeStringToByteArr(exponentString);   
            BigInteger modulus = new BigInteger(1, modulusBytes);   
            BigInteger exponent = new BigInteger(1, exponentBytes);   
  
            RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);   
            KeyFactory fact = KeyFactory.getInstance("RSA");   
            PublicKey pubKey = fact.generatePublic(rsaPubKey);   
  
            Cipher cipher = Cipher.getInstance("RSA");   
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);   
  
            byte[] ssb = ss.getBytes();
            byte[] cipherData = cipher.doFinal(ssb);   
            return cipherData;   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
        return null;   
  
    }   
  
    public static String Dencrypt(byte[] encrypted) {   
        try {   
            byte[] expBytes = BASE64.decodeStringToByteArr(delement);   
            byte[] modBytes = BASE64.decodeStringToByteArr(module);   
  
            BigInteger modules = new BigInteger(1, modBytes);   
            BigInteger exponent = new BigInteger(1, expBytes);   
  
            KeyFactory factory = KeyFactory.getInstance("RSA");   
            Cipher cipher = Cipher.getInstance("RSA");   
  
            RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, exponent);   
            PrivateKey privKey = factory.generatePrivate(privSpec);   
            cipher.init(Cipher.DECRYPT_MODE, privKey);   
            byte[] decrypted = cipher.doFinal(encrypted);   
            return new String(decrypted,"utf-8");   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
        return null;   
    }   
    
}  

